require 'test/unit'
require File.dirname(__FILE__) + '/../../lib/fit4ruby'

class TestFit4Ruby < Test::Unit::TestCase

  def setup
    @root = File.dirname(__FILE__) + "/../.."
    Fit4Ruby.base_dir = @root
    
    @test_csv_ko = "#{@root}/test/fixture/calcola_percento.rb
    CalcolaPercento
    percentuale, importo, percento?
    15, 100, 15
    30, 200, 60
    50, 200, 1"

    @test_csv_ko_output = "#{@root}/test/fixture/calcola_percento.rb
    CalcolaPercento
    percentuale, importo, percento?
    15, 100, 15#OK
    30, 200, 60#OK
    50, 200, 1#KO
"


    @test_csv_ok = "#{@root}/test/fixture/calcola_percento.rb
    CalcolaPercento
    percentuale, importo, percento?
    15, 100, 15
    30, 200, 60
    50, 200, 100"
    
    @test_csv_ok_output = "#{@root}/test/fixture/calcola_percento.rb
    CalcolaPercento
    percentuale, importo, percento?
    15, 100, 15#OK
    30, 200, 60#OK
    50, 200, 100#OK
"

    @test_csv_ko_con_commenti= "#{@root}/test/fixture/calcola_percento.rb
    CalcolaPercento#commento
    percentuale, importo, percento?
    15, 100, 15
    #commento
    30, 200, 60
    #commento
    50, 200, 1#commento"

    @test_csv_ko_con_commenti_output= "#{@root}/test/fixture/calcola_percento.rb
    CalcolaPercento#commento
    percentuale, importo, percento?
    15, 100, 15#OK
    30, 200, 60#OK
    50, 200, 1#KO
"

  end

  def test_can_load_class
    fit = Fit4Ruby.new
    fit.load "#{@root}/test/fixture/calcola_percento.rb", 'CalcolaPercento'
    assert_equal('CalcolaPercento', fit.nameLoadedClass?, 'class CalcolaPercento should be loaded')
    assert_not_nil CalcolaPercento.new
  end

  def test_can_call_methods
    fit = Fit4Ruby.new
    fit.load "#{@root}/test/fixture/calcola_percento.rb", 'CalcolaPercento'
    fit.set 'percentuale', 15
    fit.set 'importo', 100
    assert_equal 15, fit.call('percento?')
  end

  def test_can_parse_csv
    fit = Fit4Ruby.new
    fit.open_csv @test_csv_ko

    assert_equal 'CalcolaPercento', fit.nameLoadedClass?
    assert_equal 3, fit.number_of_tests?
  end

  def test_can_run_test
    fit = Fit4Ruby.new
    fit.open_csv @test_csv_ko

    assert_equal 0, fit.number_of_succeded_tests?
    assert_equal 0, fit.number_of_failed_tests?

    fit.run
    assert_equal 2, fit.number_of_succeded_tests?
    assert_equal 1, fit.number_of_failed_tests?
  end

  def test_can_run_test_in_file
    fit = Fit4Ruby.new
    fit.open_file "#{@root}/test/acceptance/fixture.rt"

    assert_equal 0, fit.number_of_succeded_tests?
    assert_equal 0, fit.number_of_failed_tests?

    fit.run
    assert_equal 2, fit.number_of_succeded_tests?
    assert_equal 1, fit.number_of_failed_tests?
  end

  def test_can_output_ko
    fit = Fit4Ruby.new
    fit.open_csv @test_csv_ko

    fit.run
    output=fit.output
    assert_equal @test_csv_ko_output,output
  end

  def test_can_output_ok
    fit = Fit4Ruby.new
    fit.open_csv @test_csv_ok

    fit.run
    output=fit.output
    assert_equal @test_csv_ok_output,output
  end
  
  

  def test_can_output_with_comments
    assert_equal """
      #commento
      non commento""", "\n      \#commento\n      non commento"
  end

  def test_can_handle_commenti_riga
    fit= Fit4Ruby.new
    fit.open_csv @test_csv_ko_con_commenti
    fit.run
    output=fit.output
    assert_equal @test_csv_ko_con_commenti_output,output
  end

  def test_remove_comments
    fit=Fit4Ruby.new
    lines=["a#a","b","#c"]
    fit.remove_comments!(lines)
    assert_equal "a", lines.first
    assert_equal "b", lines.last
  end

  def test_fix_pathname_in_fixture
    assert_equal "#@root/pippo", Fit4Ruby.fix_fixture_pathname("pippo")
    assert_equal "/pippo", Fit4Ruby.fix_fixture_pathname("/pippo")
  end
  

end
